package cn.yunhe.controller;

import cn.yunhe.entity.Tag;
import cn.yunhe.service.impl.TagServiceImpl;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TagController {
    @Resource
    private TagServiceImpl tagService;

    @RequestMapping("addTag")
    public int addTag(Tag tag) {
        return tagService.addTag(tag);

    }

    @RequestMapping("delTag")
    public int delTag(Tag tag) {
        return tagService.delTag(tag);
    }


    @RequiresPermissions("/view/tag-list.html")
    @RequestMapping("/toTag.do")
    public void toTag(HttpServletResponse response) throws IOException {
        response.sendRedirect("/view/tag-list.html");
    }

    @RequestMapping("/getPage")
    public Map queryTagByPage(int pageSize, int pageNum, Tag tag) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("tag", tag);
        Page page = tagService.getTagListByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());

        return map;

    }

    @RequestMapping("getTagById")
    public ModelAndView getTagById(Tag tag, HttpServletRequest request) {
        Tag t = tagService.getTagById(tag);
        request.setAttribute("data", t);
        return new ModelAndView("updTag");
    }


    @RequestMapping("/updTag")
    public int updTag(Tag tag) {
        System.out.println(tag.getMemo());
        return tagService.updTag(tag);

    }


    public TagServiceImpl getTagService() {
        return tagService;
    }

    public void setTagService(TagServiceImpl tagService) {
        this.tagService = tagService;
    }
}
